package com.skydroid.demo.utils

import android.content.res.Resources
import android.os.Build
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast

/**
 * author:CQD

 * e-mail:634897993@qq.com

 * data:  2020/4/22

 * desc:
 *
 * version:
 */
object ToastShow {
    private var mToast: Toast? = null

    fun showMsg(text: String) {
        showSystemToast(
            text,
            Toast.LENGTH_SHORT
        )
    }

    fun showLongMsg(text: String) {
        showSystemToast(
            text,
            Toast.LENGTH_LONG
        )
    }

    private fun showSystemToast(text: String, duration: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LibKit.getHandler()?.post { Toast.makeText(LibKit.getContext(), text, duration).show() }
        } else {
            LibKit.getHandler()?.post {
                if (mToast == null) {
                    mToast = Toast.makeText(LibKit.getContext(), text, duration)
                } else {
                    mToast!!.setText(text)
                    mToast!!.duration = duration
                }
                val textViewId = Resources.getSystem()
                    .getIdentifier("message", "id", "android")
                (mToast!!.view
                    .findViewById(textViewId) as TextView).gravity = Gravity.CENTER
                mToast!!.show()
            }
        }
    }
}